package me.shurufa.activities;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.model.BookListResponse;
import me.shurufa.net.BaseResponse;
import me.shurufa.net.NetworkConstants;
import me.shurufa.net.RequestServerTask;
import me.shurufa.utils.Global;
import me.shurufa.utils.HttpUtil;
import me.shurufa.utils.LogUtils;
import me.shurufa.utils.RecyclerViewStateUtils;
import me.shurufa.utils.Utils;
import me.shurufa.widget.LoadingFooter;

/**
 * 从SettingActivity点击反馈跳转页面
 *
 * @FileName: me.shurufa.activities.FeedBackActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-19 11:45
 */
public class FeedBackActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.right_text)
    TextView rightText;
    @Bind(R.id.feedback_edit)
    EditText feedbackEdit;

    private InputMethodManager imm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_feedback;
        super.onCreate(savedInstanceState);
        initUI();
    }

    private void initUI() {
        imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        titleTextView.setText(getString(R.string.feedback));
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(getString(R.string.send));
        rightText.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.right_text:
                imm.hideSoftInputFromWindow(feedbackEdit.getWindowToken(),0);
                final String text = feedbackEdit.getText().toString().trim();
                if (!TextUtils.isEmpty(text)&&text.length()>6){
                    new RequestServerTask<BookListResponse>(BookListResponse.class) {
                        @Override
                        protected String requestServer() {
                            Map<String, Object> params = new HashMap<String, Object>();
                            params.put("token",Global.currentUser.token);
                            params.put("content",text);
                            return HttpUtil.post(NetworkConstants.FEEDBACK, params);
                        }

                        @Override
                        protected void onSuccess(BookListResponse result) {
                            //没有更多数据 不能加载更多
                            Utils.showToast(getString(R.string.commit_success));
                            feedbackEdit.setText("");
                        }
                    }.start();
                }else if (TextUtils.isEmpty(text)){
                    Utils.showToast(R.string.content_is_empty);
                }else {
                    Utils.showToast(R.string.content_must_greater_than_6);
                }
                break;
        }
    }
}
